package com.cmcc.cmii.cmiiservicemanagement.utils;

import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author Thanks
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RequestUtils {

    private final RestTemplate restTemplate;

    public ResponseEntity<String> sendRequest(String url, String method, Map<String, Object> reqHeaders,Map<String,Object> reqParams) throws Exception{
        HttpMethod httpMethod = HttpMethod.GET;
        if("post".equalsIgnoreCase(method)){
            httpMethod = HttpMethod.POST;
        }
        HttpHeaders headers = new HttpHeaders();
        // 以什么方式提交，自行选择，一般使用json，或者表单
        reqHeaders.forEach((k,v)->{
            headers.add(k, v==null?null:v.toString());
        });
        //将请求头部和参数合成一个请求
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(reqParams, headers);
        //执行HTTP请求，将返回的结构使用Response类格式化
        return restTemplate.exchange(url, httpMethod, requestEntity, String.class);
    }
}
